home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2440 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.6 KB

  1. Path: newsroom.hitc.com!usenet
  2. From: psand@eos.hitc.com (G. Patrick Sand)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: If statements, HELP!!
  5. Date: 17 Jan 1996 20:09:25 GMT
  6. Organization: Hughes Aircraft (EOSDIS)
  7. Message-ID: <4djl1l$hrh@newsroom.hitc.com>
  8. References: <956_9601132330@medtechnet.com>
  9. NNTP-Posting-Host: 155.157.118.56
  10. Mime-Version: 1.0
  11. X-Newsreader: WinVN 0.99.3
  12.  
  13. In article <956_9601132330@medtechnet.com>, 
  14. Tony.Johnston@dreams.medtechnet.com says...
  15. >
  16. >Can anyone see in this code fragment why this if statement wont do what
  17. >I need it to do.  Using Borland C++ 4.0:
  18. >{
  19. >    if (SysopPercent > UserPercents)  <--If this is true it should fall 
  20. down 
  21. >       {                                 to the next if, if false it 
  22. should
  23. >                                         go to the ELSE
  24. >       if (SysTime < LockTime)        <--If true it should process its
  25. >                                         statement, if false it should 
  26. fall
  27. >                                         down to the else
  28. >       TDDisplayFile("LOCK.ANS");
  29. >       }
  30. >       else
  31.  
  32. 1. The else is only hit when SysopPercent <= UserPercents !!! not 
  33.    SysTime >= LockTime
  34. 2. Only the first call is done (TBDoors_Tmp.Open())...
  35.  
  36. >        TBDoors_Tmp.Open();
  37.  
  38. 3. The rest is always called because it is not enclosed in a brackets 
  39. block...
  40.  
  41. >        TBDoors_Tmp.Read();
  42. >        TBDoors_Tmp.WhereTheDoorWasCalledFrom(5);
  43. >        TBDoors_Tmp.Write();
  44. >        TBDoors_Tmp.Close();
  45. >}
  46. [snip!]
  47.  
  48. I BET YOU WERE EXECUTING THE TBDoors_Tmp.Open() call when the percentage 
  49. test failed...run the debugger and step through this logic to verify...
  50.  
  51. Recode along these lines:
  52.  
  53. // indent and align as you like...
  54.  
  55. // if it fails either condition, do the "ELSE" stuff...
  56. if ( ( SysopPercent <= UserPercents ) || ( SysTime >= LockTime ) )
  57. {
  58.         TBDoors_Tmp.Open();
  59.         TBDoors_Tmp.Read();
  60.         TBDoors_Tmp.WhereTheDoorWasCalledFrom(5);
  61.         TBDoors_Tmp.Write();
  62.         TBDoors_Tmp.Close();
  63. } else
  64. {
  65. // it passed both conditions so do the desired thing...
  66.      TDDisplayFile("LOCK.ANS");
  67. };
  68.  
  69.  
  70. I like to put brackets in the "THEN" and "ELSE" parts of the code even 
  71. when they are single lines to avoid such problems as these...
  72.  
  73. That should fix it, if I understood things correctly...
  74. -- 
  75. G. Patrick Sand
  76. psand@eos.hitc.com
  77. PatSand@aol.com
  78. (301) 925-0791
  79. "Travel Light But Right..."
  80. Microsoft Network is prohibited from redistributing 
  81. this work in any form, in whole or in part.   License 
  82. to distribute this individual post is available to Microsoft
  83. for $999. Posting without permission constitutes an 
  84. agreement to these terms...gps
  85.  
  86.